/*
Copyright SecureKey Technologies Inc. All Rights Reserved.

SPDX-License-Identifier: Apache-2.0
*/

package discovery

import "git.cloud.inspur.com/ichain/ichain-sdk-go/pkg/providers/ichain"

// FilterDiscovery 服务发现增加过滤功能
type FilterDiscovery struct {
	filter ichain.TargetFilter
	ichain.DiscoveryService
}

func NewFilterDiscovery(discover ichain.DiscoveryService, filter ichain.TargetFilter) ichain.DiscoveryService {
	if discover == nil {
		return nil
	}
	if filter == nil {
		return discover
	}
	return &FilterDiscovery{
		filter:           filter,
		DiscoveryService: discover,
	}
}

func (fd *FilterDiscovery) GetInodes() ([]ichain.Inode, error) {
	inodes, err := fd.DiscoveryService.GetInodes()
	if err != nil {
		return nil, err
	}
	filterInodes := make([]ichain.Inode, 0, len(inodes))
	for _, inode := range inodes {
		if fd.filter.Accept(inode) {
			filterInodes = append(filterInodes, inode)
		}
	}
	return filterInodes, nil
}
